package com.example.dllo.keepdemo.train.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.example.dllo.keepdemo.R;
import com.example.dllo.keepdemo.tools.VolleySingleton;
import com.example.dllo.keepdemo.train.bean.TrainCourseDetailBean;

/**
 * Created by dllo on 16/1/25.
 */
public class TrainCourseDetailPlanAdapter extends BaseAdapter {
    private Context context;
    private TrainCourseDetailBean data;

    public TrainCourseDetailPlanAdapter(Context context, TrainCourseDetailBean data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public int getCount() {
        return data.getData().getResults().getPlans() == null ? 0 : data.getData().getResults().getPlans().size() ;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 1;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyViewHolder myViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_listview_fragment_train_plan,parent,false);
            myViewHolder = new MyViewHolder(convertView);
            convertView.setTag(myViewHolder);
        } else {
            myViewHolder = (MyViewHolder) convertView.getTag();
        }

        myViewHolder.tvTitle.setText(data.getData().getResults().getPlans().get(position).getName());
        myViewHolder.tvDifficulty.setText("K" + data.getData().getResults().getPlans().get(position).getDifficulty());
        switch (data.getData().getResults().getPlans().get(position).getDifficulty()) {
            case 1:
                myViewHolder.tvFoundations.setText("零基础");
                break;
            case 2:
                myViewHolder.tvFoundations.setText("初学");
                break;
            case 3:
                myViewHolder.tvFoundations.setText("进阶");
                break;
            case 4:
                myViewHolder.tvFoundations.setText("强化");
                break;
            case 5:
                myViewHolder.tvFoundations.setText("挑战");
                break;
        }
        myViewHolder.tvEquipments.setText(data.getData().getResults().getPlans().get(position).getEquipments().get(0).getName());
        myViewHolder.tvTime.setText(data.getData().getResults().getPlans().get(position).getDays() + "节");
        myViewHolder.tvBody.setText(data.getData().getResults().getPlans().get(position).getTrainingpoints().get(0).getName());
        myViewHolder.ratingBar.setRating((float) data.getData().getResults().getPlans().get(position).getDifficulty());

        ImageLoader.ImageListener listener = ImageLoader.getImageListener(myViewHolder.imgBack, R.mipmap.plan320_160, R.mipmap.no_photo);
        VolleySingleton.getImageLoader().get(data.getData().getResults().getPlans().get(position).getPicture(), listener, 400, 460);

        return convertView;
    }

    class MyViewHolder {
        private ImageView imgBack;
        private RatingBar ratingBar;
        private TextView tvTitle, tvDifficulty, tvFoundations, tvEquipments, tvTime, tvBody, tvJoin;
        public MyViewHolder(View view) {
            tvTitle = (TextView) view.findViewById(R.id.tv_item_train_plan_title);
            tvDifficulty = (TextView) view.findViewById(R.id.tv_item_train_plan_difficulty);
            tvFoundations = (TextView) view.findViewById(R.id.tv_item_train_plan_foundations);
            tvEquipments = (TextView) view.findViewById(R.id.tv_item_train_plan_equipments);
            tvTime = (TextView) view.findViewById(R.id.tv_item_train_plan_whole_time);
            tvBody = (TextView) view.findViewById(R.id.tv_item_train_plan_whole_body);
            tvJoin = (TextView) view.findViewById(R.id.tv_item_train_plan_join_person_number);
            ratingBar = (RatingBar) view.findViewById(R.id.rating_item_train_plan_star);
            imgBack = (ImageView) view.findViewById(R.id.img_item_train_plan);
        }
    }
}
